package com.itheima.shotplane;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;

import com.itheima.shotplane.entity.Bullet;
import com.itheima.shotplane.entity.Car;
import com.itheima.shotplane.entity.Enemy;
import com.itheima.shotplane.entity.OtherCar;
import com.itheima.shotplane.listener.AddBoomListener;
import com.itheima.shotplane.listener.AddBulletListener;
import com.itheima.shotplane.listener.AddEnemyListener;

/**
 * 场景控制类
 *
 * @author Administrator
 */
public class PlaneWar extends FrameLayout {
    private String TAG = "PlaneWar";

    private Paint paint = null;
    private String str = "当前得分：%s";
    private int score = 0;// 得分
    private int textHeight = 0;
    boolean nn = false;
    private final int CREATE_ENEMY_DURATION = 1000;// 制造敌人间隔时间
    private final int CREATE_BULLET_DURATION = 1000;// 生产子弹间隔时间
    private final int MSG_ENEMY = 1;
    private final int MSG_BULLET = 2;



    public PlaneWar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public PlaneWar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PlaneWar(Context context) {
        super(context);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setTextSize(16);
        paint.setColor(Color.WHITE);
        paint.setTextSkewX(-0.15f);

        textHeight = getTextHeight(str);
    }

    public Car getmCar() {
        return mCar;
    }

    public void setmCar(Car mCar) {
        this.mCar = mCar;
    }

    /**
     * 开始游戏
     */


    private Car mCar;

    public OtherCar getOtherCar() {
        return otherCar;
    }

    public void setOtherCar(OtherCar otherCar) {
        this.otherCar = otherCar;
    }

    private OtherCar otherCar;

    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case MSG_ENEMY:
                    generateEnemy();
                    break;
                case MSG_BULLET:
                    generateBullet();
                    break;
            }
        };
    };





    /**
     * 制造敌人，每隔一段时间制造一个
     */
    private void generateEnemy() {
        Enemy enemy = new Enemy(getContext());
        enemy.setPlaneWar(this);
        enemy.getViewTreeObserver().addOnGlobalLayoutListener(
                new AddEnemyListener(enemy));
        addView(enemy);

        // 循环制造敌人
//        handler.sendEmptyMessageDelayed(MSG_ENEMY, CREATE_ENEMY_DURATION);
    }

    /**
     * 生产子弹
     */
    private void generateBullet() {
        Bullet bullet = new Bullet(getContext());
        bullet.setCar(mCar);
        bullet.getViewTreeObserver().addOnGlobalLayoutListener(
                new AddBulletListener(bullet));
        addView(bullet);
        // 循环制造子弹
         handler.sendEmptyMessageDelayed(MSG_BULLET, CREATE_BULLET_DURATION);
    }

public void start(){
    generateBullet();
}






    /**
     * 重新开始游戏
     */
    public void clearAll() {
//        handler.removeMessages(MSG_ENEMY);
//        handler.removeMessages(MSG_BULLET);
        // 清除所有游戏实体
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View view = getChildAt(i);
            if (view instanceof Enemy) {
                Enemy enemy = (Enemy) view;
                enemy.stopAnimation();
            } else if (view instanceof Bullet) {
                Bullet bullet = (Bullet) view;
                bullet.stopAnimation();
            }
        }

        removeAllViews();

        // 重置分数
        resetScore();
    }

    /**
     * 引爆敌人，此处不能用WindowManager实现，因为同时可能引爆多个
     *
     * @param translationY
     * @param translationX
     * @param
     */
    public void boomEnemy(float translationX, float translationY) {
        // 创建用于临时爆炸的敌人
        View v = new View(getContext());
        Drawable drawable = getResources().getDrawable(R.drawable.enemy_boom);
        v.setLayoutParams(new LayoutParams(drawable.getIntrinsicWidth(),
                drawable.getIntrinsicHeight()));
        v.setBackgroundDrawable(drawable);
        v.setTranslationX(translationX);
        v.setTranslationY(translationY);

        v.getViewTreeObserver().addOnGlobalLayoutListener(
                new AddBoomListener(v));
        addView(v);
    }

    /**
     * 引爆英雄
     */
    public void boomHero() {
        // 创建用于临时爆炸的英雄
        View v = new View(getContext());

        Drawable drawable = getResources().getDrawable(R.drawable.hero_boom);
        v.setLayoutParams(new LayoutParams(drawable.getIntrinsicWidth(),
                drawable.getIntrinsicHeight()));
        v.setBackgroundDrawable(drawable);
        v.setTranslationX(mCar.getTranslationX());
        v.setTranslationY(mCar.getTranslationY());

        v.getViewTreeObserver().addOnGlobalLayoutListener(
                new AddBoomListener(v));
        addView(v);
    }

    /**
     * 移除子弹
     */
    public void removeBullet() {
//         handler.removeMessages(MSG_BULLET);
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View view = getChildAt(i);
            if (view instanceof Bullet) {
                Bullet bullet = (Bullet) view;
                bullet.stopAnimation();
                removeView(bullet);
            }
        }
    }

    public void hideHero() {
        mCar.setVisibility(View.INVISIBLE);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        canvas.drawText(String.format(str, score), 1, textHeight + 1, paint);
    }

    public int getTextHeight(String text) {
        Rect bounds = new Rect();
        paint.getTextBounds(text, 0, text.length(), bounds);
        return bounds.height();
    }

    public void increaseScore() {
        score += 100;
        postInvalidate();
    }

    private void resetScore() {
        score = 0;
        postInvalidate();
    }

// ===================================================
    /**
     * 移动英雄到某个点
     */
    public void moveHeroTo(float x, float y) {
        // 计算translation坐标和场景相对坐标的偏移量
        float xOffset = x - mCar.getTranslationX() - mCar.getWidth() / 2;
        float yOffset = y - mCar.getTranslationY() - mCar.getHeight() / 2;
        moveHeroBy(xOffset, yOffset );

    }

    /**
     * 移动英雄
     *
     * @param xOffset
     * @param yOffset
     */
    private void moveHeroBy(float xOffset, float yOffset ) {
        float newTranslationX = mCar.getTranslationX() + xOffset;
        float newTranslationY = mCar.getTranslationY() + yOffset;


        // 控制英雄的四边不能超出场景范围
        if (newTranslationX < 0)
            newTranslationX = 0;
        if (newTranslationX > (getWidth() - mCar.getWidth())) {
            newTranslationX = getWidth() - mCar.getWidth();
        }


        if (newTranslationY < 0)
            newTranslationY = 0;
        if (newTranslationY > (getHeight() - mCar.getHeight())) {
            newTranslationY = getHeight() - mCar.getHeight();
        }

//        Log.e("测试", "newTranslationX=" + newTranslationX);
        mCar.setTranslationX(newTranslationX);
        mCar.setTranslationY(newTranslationY);

    }
//============================================
    /**
     * 移动英雄到某个点
     */
    public void moveHeroToOther(float x, float y) {
        // 计算translation坐标和场景相对坐标的偏移量
        float xOffset = x - otherCar.getTranslationX() - otherCar.getWidth() / 2;
        float yOffset = y - otherCar.getTranslationY() - otherCar.getHeight() / 2;
        moveHeroByotherCar(xOffset, yOffset );

    }

    /**
     * 移动英雄
     *
     * @param xOffset
     * @param yOffset
     */
    private void moveHeroByotherCar(float xOffset, float yOffset ) {
        float newTranslationX = otherCar.getTranslationX() + xOffset;
        float newTranslationY = otherCar.getTranslationY() + yOffset;


        // 控制英雄的四边不能超出场景范围
        if (newTranslationX < 0)
            newTranslationX = 0;
        if (newTranslationX > (getWidth() - otherCar.getWidth())) {
            newTranslationX = getWidth() - otherCar.getWidth();
        }


        if (newTranslationY < 0)
            newTranslationY = 0;
        if (newTranslationY > (getHeight() - otherCar.getHeight())) {
            newTranslationY = getHeight() - otherCar.getHeight();
        }

//        Log.e("测试", "newTranslationX=" + newTranslationX);
        otherCar.setTranslationX(newTranslationX);
        otherCar.setTranslationY(newTranslationY);

    }

}
